home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / amos / jwindows.lha / me.Asc < prev    next >
Text File  |  1996-04-25  |  8KB  |  236 lines

  1. '********************************************************************* 
  2. '* 
  3. '*   Me
  4. '* 
  5. '********************************************************************* 
  6.  
  7. '   This little program demonstrates simple file handling. It opens a window 
  8. 'in which the user can type his/her name, age and weight. They can then save 
  9. 'this and load for viewing at another time.
  10.  
  11. '   In the loading/saving procedures an attempt is made at more realistic
  12. 'file handling. If an error occurs, a requester appears to tell the user that
  13. 'the file cannot be loaded/saved. It does not jump to _CLEANUP and end the 
  14. 'program. We also place an ID string at the start of the data file to identify 
  15. 'files from this program. Finally, we keep track of whether the user has 
  16. 'changed any data, and if he tries to load new data over this, we bring up 
  17. 'a 'Load? All data will be lost" requester.
  18.  
  19. '********************************************************************* 
  20.  
  21. Global _SCRAPTAGS,SCRAPTAGS,_PORTLIST,_MESSLIST
  22. Global PATH$,OSVER
  23. Global FHEIGHT,FWIDTH,MBAR,OX,OY,SW,SH
  24. Dim _MEGADS(6)
  25. Global _MEGADS()
  26. 'Oooops!! Made a spelling mistake in the GadToolsBox editor. _AGW should 
  27. 'read _AGE. Well, I can live with that...
  28. Global _NAMESTRING,_SURSTRING,_AGW,_WEIGHT,_SAVE,_LOAD
  29. Dim _MEZOOM(1)
  30. Global _MEZOOM()
  31. Global _MEWIND
  32.  
  33. '** MYASL is a pointer to an ASL file requester we'll make later 
  34. '   LOOSE keeps track of whether there is loosable data. False=No, True=Yes
  35. Global MYASL
  36. Global LOOSE
  37.  
  38. On Error Proc _CLEANUP
  39.  
  40. '** These two strings are the title texxts for the file requester, depending 
  41. '   on whether we are loding or saving. This way, the strings are only 
  42. '   defined once.  
  43. Global _LOADTXT,_SAVETXT
  44. _LOADTXT=J Make String("Select a Me file to load")
  45. _SAVETXT=J Make String("Save file as")
  46.  
  47. _INITIALIZE
  48. _GUIDATA
  49. _SETUPALL
  50. _SETPORTS
  51.  
  52. '** here we create the file requester, with a few appropriate tags.
  53. 'Note that this is done AFTER opening the window otherwise ASLFR_Window
  54. 'would be rather pointless.
  55. 'Remember to add a J Free Asl Requester line to the _CLEANUP routine 
  56.  
  57. J Tag _SCRAPTAGS,1,Equ("ASLFR_TitleText"),J Make String("Select a med file")
  58. J Tag Equ("ASLFR_SleepWindow"),True
  59. J Tag Equ("ASLFR_Window"),_MEWIND
  60. J Tag Equ("ASLFR_RejectIcons"),True
  61. J Tag 0,0
  62. MYASL=J Create Asl Requester(Equ("ASL_FileRequest"),_SCRAPTAGS)
  63.  
  64. Do 
  65.    K=J Wait Message
  66.    While K
  67.       C=J Tag Data(_MESSLIST,1)
  68.       If C=Equ("IDCMP_CLOSEWINDOW")
  69.          If J Easy Request(_MEWIND,"Me","Do you really"+Chr$(10)+"want to quit?"," Quit | Cancel ",0)
  70.             _CLEANUP
  71.          End If 
  72.       Else If C=Equ("IDCMP_REFRESHWINDOW")
  73.          _DOREFRESH
  74.       Else If C=Equ("IDCMP_GADGETUP")
  75.          _HANDLEGADGETS
  76.       End If 
  77.       K=J Next Message
  78.    Wend 
  79. Loop 
  80.  
  81. '** This routine handles all the gadget presses
  82. Procedure _HANDLEGADGETS
  83.    On Error Proc _CLEANUP
  84.    
  85.    G=J Tag Data(_MESSLIST,4)
  86.    
  87.    If G=_MEGADS(_SAVE)
  88.       'save the data:
  89.       'first, we set up and call the file requester. We then save the file 
  90.       'with the _SAVEFILE procedure.   
  91.       J Tag SCRAPTAGS,1,Equ("ASLFR_DoSaveMode"),True
  92.       J Tag Equ("ASLFR_TitleText"),_SAVETXT
  93.       J Tag 0,0
  94.       F$=J File Request$(MYASL,SCRAPTAGS)
  95.       If F$<>""
  96.          _SAVEFILE[F$]
  97.       End If 
  98.    Else If G=_MEGADS(_LOAD)
  99.       'Load some data
  100.       'R states whether it's ok to load data over what's there. If LOOSE is
  101.       'false, it's automatically true, but if LOOSE is true, we bring up 
  102.       'a requester for the user and let him decide.
  103.       'Then set up the file requester and get a file. load this. 
  104.       R=True
  105.       If LOOSE
  106.          R=J Easy Request(_MEWIND,"Load?","All present data"+Chr$(10)+"will be lost!"," Ok | Cancel ",0)
  107.       End If 
  108.       If R
  109.          J Tag SCRAPTAGS,1,Equ("ASLFR_DoSaveMode"),False
  110.          J Tag Equ("ASLFR_TitleText"),_LOADTXT
  111.          J Tag 0,0
  112.          F$=J File Request$(MYASL,SCRAPTAGS)
  113.          If F$<>""
  114.             _LOADFILE[F$]
  115.          End If 
  116.       End If 
  117.    Else 
  118.       'If any other gadget has been selected, the data is presumed to have 
  119.       'changed, and LOOSE is set to TRUE. The data gadgets don't need handling.
  120.       LOOSE=True
  121.    End If 
  122.    
  123. End Proc
  124. Procedure _SAVEFILE[F$]
  125.    On Error Proc _CLEANUP
  126.    
  127.    'Open the file if possible. Note how we trap an error if it occurs 
  128.    'then jump to E, rather than quiting.
  129.    Trap F=J Open(F$,Equ("MODE_NEWFILE"))
  130.    If Errtrap Then Goto E
  131.    
  132.    'Initially, we set the ID of the file to Err!. If the load procedure 
  133.    'finds this, it will know there was a writing error. We change this  
  134.    'after writing the data. 
  135.    Trap J Write Ascii$ F,"Err!"
  136.    If Errtrap Then Goto E
  137.  
  138.    'Here, we write in the four items of data, reading the directly from the 
  139.    'gadgets as we go. Note that we write 2 strings, then 2 numbers. Reading 
  140.    'must take place in this order.
  141.    Trap J Write$ F,J Read String$(J Gadget String(_MEGADS(_NAMESTRING)))
  142.    If Errtrap Then Goto E
  143.    Trap J Write$ F,J Read String$(J Gadget String(_MEGADS(_SURSTRING)))
  144.    If Errtrap Then Goto E
  145.    Trap J Write F,J Gadget Number(_MEGADS(_AGW))
  146.    If Errtrap Then Goto E
  147.    Trap J Write F,J Gadget Number(_MEGADS(_WEIGHT))
  148.    If Errtrap Then Goto E
  149.  
  150.    'Now the writing is done, we can go back to the start of the file and write  
  151.    'in a valid ID string. Note that this MUST be the same length. 4 characters
  152.    'is conventional.
  153.    J Set Pof F,0
  154.    J Write Ascii$ F,"Jme."
  155.    
  156.    'Close the file, set LOOSE to false and quit.
  157.    If F Then F=J Close(F)
  158.    LOOSE=False
  159.    Pop Proc
  160.    
  161.    E:
  162.    'If an error has occured, close the file if open and put up an error 
  163.    'requester. This doesn't actually request, it informs, but that's  
  164.    'rather pedantic.
  165.    If F Then F=J Close(F)
  166.    V=J Easy Request(_MEWIND,"Error!","Couldn't save"+Chr$(10)+"the file!","Cancel",0)
  167. End Proc
  168. Procedure _LOADFILE[F$]
  169.    On Error Proc _CLEANUP
  170.    
  171.    'Try and open the file 
  172.    Trap F=J Open(F$,Equ("MODE_OLDFILE"))
  173.    If Errtrap Then Goto E
  174.    
  175.    'Test the ID string of the file. If it's not what it should be, goto the 
  176.    'error routine. Note the use of E$. This allows us to specify the error
  177.    'better for the users benefit. If E$ is not set, a default string is 
  178.    'displayed (see later).  
  179.    A$=J Read Ascii$(F,4)
  180.    If A$="Err!"
  181.       E$="There was an error"+Chr$(10)+"when writing this file!"
  182.       Goto E
  183.    Else If A$<>"Jme."
  184.       E$="This is not"+Chr$(10)+"one of my files!"
  185.       Goto E
  186.    End If 
  187.    
  188.    'Read off the data from the file. As long as the file is open and is 
  189.    'a 'Me' file, there is no real need to trap the data. If something 
  190.    'really goes wrong, the program quits, but this should never happen. 
  191.    N$=J Read$(F)
  192.    S$=J Read$(F)
  193.    A=J Read(F)
  194.    W=J Read(F)
  195.    
  196.    If F Then F=J Close(F)
  197.  
  198.    'Here, we set the data in the four gadgets to the new value. Since 
  199.    'the string gadgets will copy the string, a static string is unnecessary.  
  200.    J Tag SCRAPTAGS,1,Equ("GTST_String"),Varptr(N$)
  201.    J Tag 0,0
  202.    J Set Gadget Data _MEGADS(_NAMESTRING),_MEWIND,SCRAPTAGS
  203.    J Tag SCRAPTAGS,1,Equ("GTST_String"),Varptr(S$)
  204.    J Set Gadget Data _MEGADS(_SURSTRING),_MEWIND,SCRAPTAGS
  205.    J Tag SCRAPTAGS,1,Equ("GTIN_Number"),A
  206.    J Set Gadget Data _MEGADS(_AGW),_MEWIND,SCRAPTAGS
  207.    J Tag SCRAPTAGS,1,Equ("GTIN_Number"),W
  208.    J Set Gadget Data _MEGADS(_WEIGHT),_MEWIND,SCRAPTAGS
  209.    
  210.    LOOSE=False
  211.    Pop Proc
  212.    
  213.    E:
  214.    'if the file is open, close it. If E$ is unset, use a default. Finally,
  215.    'display E$ in a requester.
  216.    If F Then F=J Close(F)
  217.    If E$=""
  218.       E$="Couldn't load"+Chr$(10)+"the file!"
  219.    End If 
  220.    V=J Easy Request(_MEWIND,"Error!",E$," Cancel ",0)
  221.    
  222. End Proc
  223.  
  224. 'Gadstools procs 
  225.  
  226. Procedure _INITIALIZE
  227. Procedure _SETUPALL
  228. Procedure _GUIDATA
  229. Procedure _MAKEMEGADS
  230. Procedure _MAKEMEWIND[SC]
  231. Procedure _MERENDER
  232. Procedure _DOREFRESH
  233. Procedure _SETPORTS
  234. Procedure _FREEWIND[W,G,M,A,C]
  235. Procedure _CLEANUP
  236.